//
//  KKPickerView.m
//  FreightAssistant
//
//  Created by leo on 15/11/22.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKPickerView.h"

@interface KKPickerView() <UIPickerViewDataSource, UIPickerViewDelegate> {
    
    BOOL _isShow;
    UILabel *_titleLabel;
    
    UIPickerView *_pickerView;
    NSString *_selectedString;
}

@end

@implementation KKPickerView

#pragma mark - Public methods

- (nullable instancetype)initWithItemTitleArray:(NSArray* __nullable)titles {
    
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat height = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    
    _titles = [NSArray arrayWithArray: titles];
    
    self = [super initWithFrame: CGRectMake(0, height, width, 260)];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.windowLevel = UIWindowLevelAlert + 1;
        
        _pickerView = [[UIPickerView alloc] initWithFrame: CGRectMake(0, 44, width, 216)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        [self addSubview: _pickerView];
        
        {
            UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, width, 44)];
            {
                UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target: nil action: nil];
                leftSpace.width = 10;
                UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @selector(actionOfCancel:)];
                
                UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
                
                UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemSave target: self action: @selector(actionOfSure:)];
                UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target: nil action: nil];
                rightSpace.width = 10;
                
                [toolBar setItems: @[leftSpace, leftItem, spaceItem, rightItem, rightSpace]];
                
                _titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(80, 12, width - 160, 20)];
                _titleLabel.backgroundColor = [UIColor clearColor];
                _titleLabel.font = [UIFont systemFontOfSize: 16];
                _titleLabel.textAlignment = NSTextAlignmentCenter;
                _titleLabel.textColor = [UIColor darkGrayColor];
                [toolBar addSubview: _titleLabel];
            }
            [self addSubview: toolBar];
        }
        self.hidden = true;
    }
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}

- (void)show {
    
    [self showWithTitle: nil];
}

- (void)showWithTitle:(NSString*)title {
    
    if (_isShow) {
        return;
    }
    
    _titleLabel.text = title;
    _isShow = true;
    
    self.hidden = false;
    __weak typeof(self) weakself = self;
    
    [_pickerView reloadAllComponents];
    
    [self makeKeyAndVisible];
    
    CGFloat height = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat selfHeight = CGRectGetHeight(self.bounds);
    
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [UIView animateWithDuration: 0.25 animations:^{
        weakself.center = CGPointMake(width/2, height - selfHeight/2);
    } completion:^(BOOL finished) {
        
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }];
}


- (void)cancel {
    
    if (!_isShow) {
        return;
    }
    __weak typeof(self) weakself = self;
    
    CGFloat height = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat selfHeight = CGRectGetHeight(self.bounds);
    
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [UIView animateWithDuration: 0.25 animations:^{
        weakself.center = CGPointMake(width/2, height + selfHeight/2);
    } completion:^(BOOL finished) {
        
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
        weakself.hidden = true;
        _isShow = false;
        
        [weakself resignKeyWindow];
    }];
}

#pragma mark - UIPickerView methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return _titles.count;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return _titles[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    _selectedString = _titles[row];
}

#pragma mark - Action methods

- (void)actionOfCancel:(UIBarButtonItem *)item {
    
    [self cancel];
    
    if (_cancleBlock) {
        _cancleBlock();
    }
}

- (void)actionOfSure:(UIBarButtonItem *)item {
    
    [self cancel];
    
    if (_saveBlock) {
        _saveBlock(_selectedString);
    }
}

@end
